<?php
/**
 * Create by PhpStorm
 * User: mozongyou
 * Date: 2021/8/16
 * Time: 9:20 下午
 */

/**
 *
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 * @author mozongyou
 * @date 2021-08-16 9:20 下午
 */
declare(strict_types=1);

namespace App\Service\WebSocket;

use Hyperf\Di\Annotation\Inject;
use Hyperf\WebSocketServer\Sender;

trait PushService
{
    /**
     * @Inject
     * @var Sender
     */
    protected $sender;

    /**
     * 返回的数据结构
     * @param $api
     * @param $requestId
     * @param null $data
     * @param int $code
     * @param string $msg
     * @return false|string
     * @author mozongyou
     * @date 2021-08-27 9:04 下午
     */
    public function json($api, $requestId, $data = null, $code = 1, $msg = '成功')
    {
        $result = [
            'api' => $api,
            'request_id' => (int)$requestId,
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
        return json_encode($result, JSON_UNESCAPED_UNICODE);
    }
}